<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">系统管理</el-breadcrumb-item>
          <el-breadcrumb-item>部门管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-input v-model="dto.name" placeholder="请输入内容" clearable></el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" icon="el-icon-search" @click="getDepartmentList()">搜索</el-button>
        </el-col>
        <el-col :span="5">
          <el-button type="success" round @click="handleAdd()">添加部门</el-button>
        </el-col>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="17">
          <el-table
            :data="departmentList"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="50">
            </el-table-column>
            <el-table-column
              prop="name"
              label="部门名称"
              width="150">
            </el-table-column>
            <el-table-column
              prop="updatetime"
              label="更新时间"
              width="150">
            </el-table-column>
            <el-table-column
              label="状态"
              width="120">
              <template slot-scope="scope">
                <span style="color: green;" v-if="scope.row.status == 1">启用</span>
                <span style="color: red;" v-else>停用</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="300">
              <template slot-scope="scope">
                <el-button
                  size="mini" type="primary" icon="el-icon-edit"
                  @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 1" type="warning"
                  @click="handleDisable(scope.row)">停用</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 0" type="success"
                  @click="handleEnable(scope.row)">启用</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="dto.page"
            :page-size="dto.size"
            layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表单弹出框 -->
    <el-dialog :title="formtitle" :visible.sync="dialogDepartmentVisible" width="30%" @close="close('ruleForm')">
      <el-form :model="department" :rules="rules" ref="ruleForm">
        <el-form-item label="部门名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="department.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    var checkName = (rule, value, callback) => {
      const reg = /^[\u4e00-\u9fa5]+$/;
      if (value.length == 0) {
        callback(new Error('请输入部门名称'));
      } else if (!reg.test(value)) {
        callback(new Error('请输入正确的部门名称'));
      } else {
        this.$axios.get("/department/checkName/" + this.department.name).then(res=>{
          if (res.data.code == 200) {
            if (res.data.data) {
              callback();
            } else {
              callback(new Error('该部门名称已存在'));
            }
          }
        })
      }
    }
    return {
      dto:{
        name:"",
        page:1,
        size:6
      },
      departmentList:[],
      total:"",
      dialogDepartmentVisible:false,
      formLabelWidth:"80px",
      formtitle:"",
      department:{
        id:"",
        name:"",
        status:"",
      },
      rules: {
        name: [
          { validator:checkName, trigger: 'blur' }
        ]
      },
    }
  },
  mounted() {
    this.getDepartmentList()
  },
  methods: {
    handleCurrentChange(val){
      this.dto.page = val
      this.getDepartmentList()
    },
    getDepartmentList(){
      this.$axios.get('/department/getDepartmentList',{
        params:{
          name: this.dto.name,
          page: this.dto.page,
          size: this.dto.size
        }}).then(res=>{
        if (res.data.code == 200) {
          this.departmentList = res.data.data.data
          this.total =res.data.data.total
        }
      })
    },
    close(formName){
      this.$refs[formName].clearValidate();
      this.dialogDepartmentVisible = false
    },
    handleAdd(){
      this.department = {
        id:"",
        name:"",
        status:"",
      }
      this.dialogDepartmentVisible = true
      this.formtitle = "添加部门"
    },
    handleEdit(row){
      this.dialogDepartmentVisible = true
      this.formtitle = "编辑部门"
      this.department.name = row.name
      this.department.status = row.status
      this.department.id = row.id
    },
    handleConfirm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formtitle == '添加部门') {
            this.$axios.post('/department/addDepartment',this.department).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '添加成功！',
                  type: 'success'
                });
                this.dialogDepartmentVisible = false
                this.getDepartmentList()
              }
            })
          }
          if (this.formtitle == '编辑部门') {
            this.$axios.put('/department/editDepartment',this.department).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '编辑成功！',
                  type: 'success'
                });
                this.dialogDepartmentVisible = false
                this.getDepartmentList()
              }
            })
          }     
        } else {
          return false;
        }
      }); 
    },
    handleEnable(row){
      this.$axios.put('/department/enableDepartment/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '启用成功!'
          });
          this.getDepartmentList()
        }
      })
    },
    handleDisable(row){
      this.$axios.put('/department/disableDepartment/' + row.id).then(res=>{
        if (res.data.code == 200) {
          if (res.data.data) {
            this.$message({
              type: 'success',
              message: '停用成功!'
            });
            this.getDepartmentList()
          }        
        }
        if(res.data.code == 500){
          this.$message.error(res.data.message + '停用失败！');
        }
      })
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/department/deleteDepartment/' + row.id).then(res=>{
          if (res.data.code == 200) {
            if (res.data.data){
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getDepartmentList()
            }
          }
          if(res.data.code == 500){
            this.$message.error(res.data.message + '删除失败！');
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
}
</script>

<style scoped>

</style>